<template>
  <div>
    <basic-modal :modalOpt="modal" :footer="null" @closeModal="closeModal">
     
      <template>
        <basic-form
        ref="forms"
          :showAlert="false"
          message=""
          :formInfos="s_FormInfo"
          :formDatas="s_formDatas"
          :rules="s_rules"
          @onSubmit="onSubmit"
          @onChangeType="onChangeType"
          @closeModal="closeModal"
        ></basic-form>
      </template>
    </basic-modal>
  </div>
</template>

<script>
import basicModal from '../common/basicModal'
import basicForm from '../common/basicForm'
import { getInfoByPid as getSupplierInfoByPid } from '@/views/app/biz/supplier/supplier'
import { getInfoByPid, saveInfo } from './purchaseOrder'
import {
  s_FormInfo,
  s_formDatas,
  s_rules
} from './dataConfig'

export default {
  components: {
    basicModal,
    basicForm,
  },
  data() {
    return {
      loading: false,
      modal: {
        modalVisible: false,
        modalTitle: '新增证照',
        modalWidth: '1000px',
      },
     s_FormInfo: s_FormInfo,
     s_formDatas:s_formDatas,
     s_rules: s_rules,
    }
  },
  computed: {},
  mounted() {},
  methods: {
    // 新增按钮 点击了新增
    addModal() {
      console.log('点击了新增')
      this.modal.modalVisible = true
      this.modal.modalTitle = '新增证照'
    },
    // 编辑按钮 点击了编辑
    editModal(val) {
      console.log('点击了编辑')
      this.getSBypid(val)
      this.modal.modalVisible = true
      this.modal.modalTitle = '修改采购单信息'
    },
    // 关闭模态详细框
    closeModal() {
      this.modal.modalVisible = false
      this.$emit('close')
    },
    // 保存成功关闭模态详细框
    closeModal2() {
      this.modal.modalVisible = false
      this.$emit('ok')
    },
    // 模态详细框提交按钮
    onSubmit(val) {
      this.saveObjInfo(val)
    },
    // 保存详细信息
    async saveObjInfo(val) {
      this.loading = true
      // this.$message.loading({ content: '提交中'， })
      let res = await saveInfo(val)
      this.loading = false
      console.log(res)
      if (res && res.code && res.code === 'C1' && res.data) {
        this.$message.success({ content: '提交成功', duration: 2 })
        this.closeModal2()
      } else {
        this.$message.error({ content: '提交失败', duration: 2 })
      }
    },
    // 得到初始化信息
    async getSBypid(val) {
      let res = await getInfoByPid(val)
      if (res && res.code && res.code === 'C1') {
        if (res.data) {
          this.s_formDatas = res.data
        } else {
          this.addModal()
        }
      }
      console.log(res)
    },
    // 搜索栏选择下拉select触发搜索val[0]表示选中的record中的pid,val[1]表示选中的record全部信息
    onChangeType(name, val) {
      console.log('formName',name)
      console.log('val',val)
      this.s_formDatas=this.$refs.forms.form
      switch (name) {
        case 'deliveryAddr': 
          if (!val[0]) {
            this.s_formDatas.deliveryAddr =null
            this.s_formDatas.delivery=null
            this.s_formDatas.deliveryTel=null
 
          }else{
            this.s_formDatas.deliveryAddr =val[1].addr
            this.s_formDatas.delivery =val[1].contact
            this.s_formDatas.deliveryTel =val[1].contactTel
          }
          break 
      }
    },
    // 获取供应商信息
    async getCustomerInfo(pid) {
      const _this = this
      const res = await getSupplierInfoByPid(pid)
      console.log(res);
      if (res && res.code && res.code === 'C1' && res.data) {
        _this.s_formDatas.keHuPid = res.data.pid
        _this.s_formDatas.keHuOrgPid = res.data.keHuOrgPid
        _this.s_formDatas.keHudm = res.data.keHudm
        _this.s_formDatas.keHumc = res.data.keHumc
        
      }
    },
    //
    restData(val) {
      this.getSBypid(this.s_formDatas.pid)
    },
  },
}
</script>

<style>
</style>